package cn.shop.merchant.controller.blacklist;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.req.BlacklistQueryReq;
import cn.shop.merchant.service.BlacklistService;
import cn.shop.transcation.blacklist.entity.Blacklist;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-10 11:14
 * @description : 黑名单controller
 */
@RestController
@RequestMapping("/blacklist")
public class BlacklistController {

    @Resource
    private BlacklistService blacklistService;



    /**
     * 新增黑名单
     *
     * @param blacklist 黑名单
     * @return 返回结果
     */
    @PostMapping("")
    @OperationLog(desc = "新增黑名单", type = OperationType.CREATE)
    public ApiResult<Map<String, Object>> insert(@Validated @RequestBody Blacklist blacklist) {
        boolean isSuccess = blacklistService.save(blacklist);
        return ApiResult.result(isSuccess);
    }

    /**
     * 更新黑名单
     *
     * @param blacklist 黑名单
     * @return 返回结果
     */
    @PutMapping("")
    @OperationLog(desc = "修改黑名单", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> update(@RequestBody Blacklist blacklist) {
        long uid = StpUtil.getSession().getLong(UID);
        boolean isSuccess = blacklistService.updateBlacklist(blacklist, uid);
        return ApiResult.result(isSuccess);
    }

    /**
     * 获取黑名单列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/list")
    public ApiResult<Map<String, Object>> list(BlacklistQueryReq req) {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> data = blacklistService.blacklists(uid, req);
        return ApiResult.success(data);
    }

    /**
     * 删除黑名单
     *
     * @param ids 编号
     * @return 返回
     */
    @DeleteMapping("/delete/{ids}")
    @OperationLog(desc = "删除黑名单", type = OperationType.DELETE)
    public ApiResult<Map<String, Object>> delete(@PathVariable("ids") long[] ids) {
        long uid = StpUtil.getSession().getLong(UID);
        boolean isSuccess = blacklistService.deleteBlacklist(ids, uid);
        return ApiResult.result(isSuccess);
    }

    /**
     * 获取黑明单信息
     *
     * @param id 编号
     * @return 返回结果
     */
    @GetMapping("/edit/{id}")
    public ApiResult<Map<String, Object>> info(@PathVariable("id") long id) {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> data = blacklistService.info(id, uid);
        return ApiResult.success(data);
    }
}
